package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysStudentsMapper;
import com.ruoyi.system.domain.SysStudents;
import com.ruoyi.system.service.ISysStudentsService;


import javax.validation.Validator;
/**
 * 学生信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-01
 */
@Service
public class SysStudentsServiceImpl implements ISysStudentsService 
{
    @Autowired
    private SysStudentsMapper sysStudentsMapper;


    @Autowired
    protected Validator validator;
    private static final Logger log = LoggerFactory.getLogger(SysStudentsServiceImpl.class);
    /**
     * 查询学生信息
     * 
     * @param studentId 学生信息主键
     * @return 学生信息
     */
    @Override
    public SysStudents selectSysStudentsByStudentId(Long studentId)
    {
        return sysStudentsMapper.selectSysStudentsByStudentId(studentId);
    }

    /**
     * 查询学生信息列表
     * 
     * @param sysStudents 学生信息
     * @return 学生信息
     */
    @Override
    public List<SysStudents> selectSysStudentsList(SysStudents sysStudents)
    {
        return sysStudentsMapper.selectSysStudentsList(sysStudents);
    }

    /**
     * 新增学生信息
     * 
     * @param sysStudents 学生信息
     * @return 结果
     */
    @Override
    public int insertSysStudents(SysStudents sysStudents)
    {
        return sysStudentsMapper.insertSysStudents(sysStudents);
    }

    /**
     * 修改学生信息
     * 
     * @param sysStudents 学生信息
     * @return 结果
     */
    @Override
    public int updateSysStudents(SysStudents sysStudents)
    {
        return sysStudentsMapper.updateSysStudents(sysStudents);
    }

    /**
     * 批量删除学生信息
     * 
     * @param studentIds 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentsByStudentIds(Long[] studentIds)
    {
        return sysStudentsMapper.deleteSysStudentsByStudentIds(studentIds);
    }

    /**
     * 删除学生信息信息
     * 
     * @param studentId 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentsByStudentId(Long studentId)
    {
        return sysStudentsMapper.deleteSysStudentsByStudentId(studentId);
    }


    /**
     * 通过学号查询学生
     *
     * @param student_number 学号
     * @return 用户对象信息
     */
    @Override
    public SysStudents selectSysStudentsByStudentNumber(String student_number)
    {
        return sysStudentsMapper.selectSysStudentsByStudentNumber(student_number);
    }

    /**
     * 导入用户数据
     *
     * @param ddList 数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStudents(List<SysStudents> ddList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(ddList) || ddList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysStudents user : ddList)
        {
            try
            {
                SysStudents u = sysStudentsMapper.selectSysStudentsByStudentNumber(user.getStudentNumber());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setCreateBy(operName);
                    user.setCreateTime(DateUtils.getNowDate());
                    sysStudentsMapper.insertSysStudents(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、订单编号 " + user.getStudentName()+"导入成功");
                }else if (isUpdateSupport)
                {

                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    user.setStudentId(u.getStudentId());
                    user.setCreateTime(DateUtils.getNowDate());
                    sysStudentsMapper.updateSysStudents(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、订单编号 " + user.getStudentName() + " 更新成功");
                }else
                {

                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、订单编号 " + user.getStudentName() + " 已存在");
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、订单编号 " + user.getStudentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
